//
//  BIDNoticeListViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/5/20.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDNoticeListViewController.h"
#import "BIDNoticeCell.h"
#import "BIDNoticeDetailViewController.h"

//通知列表
static NSString *noticeListURL = @"noticeAndroid/receive/list.action";
//通知详情
static NSString *noticeDetailURL = @"noticeAndroid/receive/getById.action";
//删除通知
static NSString *deleteNoticeURL = @"noticeAndroid/receive/remove.action";

@interface BIDNoticeListViewController ()<BIDBaseListViewDelegate>
{
    BOOL _bRegister;
    CGFloat _offsetY;
}
@end

@implementation BIDNoticeListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"通知";
    _offsetY = 0;
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.curPage = 0;
    [self.dataSourceArr removeAllObjects];
    [self.myTableView setContentOffset:CGPointMake(0, _offsetY)];
    [self loadData];
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], noticeListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"memberId=%@&page=%ld&rows=%ld", [AppDelegate getUserId], self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            NSArray *arr = responseDic[@"rows"];
            [self.dataSourceArr addObjectsFromArray:arr];
            if(self.dataSourceArr.count < self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
            [self.myTableView reloadData];
        }
        [self autoDecideToShowEmptyView];
    }];
}

/**
 *  删除某条已收通知
 *
 *  @param indexPath <#indexPath description#>
 */
- (void)deleteNoticeAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    NSDictionary *dic = self.dataSourceArr[row];
    NSString *noticeId = dic[@"id"];
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], deleteNoticeURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"ids=%@", noticeId];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary* responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            //删除成功
            [self.dataSourceArr removeObjectAtIndex:row];
            [self.myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }];
}


#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDNoticeCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDNoticeCell *noticeCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = self.dataSourceArr[row];
    NSDictionary *noticeDic = dic[@"notice"];
    NSNumber *value = dic[@"isRead"];
    if([value intValue] == 1)
    {
        noticeCell.flagLabel.text = @"[已读]";
        noticeCell.flagLabel.textColor = RGBACOLOR(39.f, 128.f, 254.f, 1.f);
    }
    else
    {
        noticeCell.flagLabel.text = @"[未读]";
        noticeCell.flagLabel.textColor = RGBACOLOR(255.f, 16.f, 0.f, 1.f);
    }
    noticeCell.titleLabel.text = noticeDic[@"title"];
    NSString *strCreator = noticeDic[@"createUserName"];
    NSString *strDate = noticeDic[@"createTime"];
    NSDate *date = [BIDGlobalMethods transToNSDateByFormatter:@"yyyy-MM-dd HH-mm-ss" fromStringDate:strDate];
    NSString *strFormatDate = [date formattedDateDescription];
    noticeCell.detailLabel.text = [[NSString alloc] initWithFormat:@"发布人:%@   %@", strCreator, strFormatDate];
    cell = noticeCell;
    cell.textLabel.text = dic[@"name"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSUInteger row = indexPath.row;
    NSDictionary *dic = self.dataSourceArr[row];
    NSDictionary *noticeDic = dic[@"notice"];
    BIDNoticeDetailViewController *vc = [[BIDNoticeDetailViewController alloc] initWithNibName:@"BIDLoadWebPageViewController" bundle:nil];
    vc.requestURL = [[NSString alloc] initWithFormat:@"%@/%@?noticeReceive.id=%@", [AppDelegate getServerAddr], noticeDetailURL, dic[@"id"]];
    NSString *strTitle = noticeDic[@"title"];
    NSString *strCreator = noticeDic[@"createUserName"];
    NSString *strDate = noticeDic[@"createTime"];
    NSDateFormatter *dateFormatter = [NSDateFormatter defaultDateFormatter];
    NSDate *date = [dateFormatter dateFromString:strDate];
    NSString *strFormatDate = [date formattedDateDescription];
    NSString *strPublishInfo = [[NSString alloc] initWithFormat:@"发布人:%@   %@", strCreator, strFormatDate];
    NSMutableString *strExternalContent = [NSMutableString string];
    if(strTitle.length > 0)
    {
        [strExternalContent appendFormat:@"<p style='margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: normal; color: rgb(0, 0, 0); font-size: 20px; line-height: normal; font-family: 微软雅黑; font-weight:900, widows: 1;'>%@</p>", strTitle];
    }
    if(strPublishInfo.length > 0)
    {
        [strExternalContent appendFormat:@"<p style='margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: normal; color: rgb(0, 0, 0); font-size: 11px; line-height: normal; font-family: 微软雅黑; widows: 1;'>%@</p><br/>", strPublishInfo];
    }
    vc.externalContent = strExternalContent;
    _offsetY = self.myTableView.contentOffset.y;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 66.f;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        //删除某条通知
        [self deleteNoticeAtIndexPath:indexPath];
    }
}

@end
